# -*- coding: utf-8 -*-
"""
Created on Wed Mar 12 19:46:29 2014

@author: Maxim
"""
from nose_dialog import Ui_nose_profile
from PyQt4 import QtGui
from nose_shapes import *

class NoseShape():
    def __init__(self):
        self.type      = 0
        self.blunt     = None
        self.Rnose     = 0.0
        self.parameter = None

    def __call__(self,noseLength=1.0, noseDiameter=1.0):
        L = float(noseLength)
        D = float(noseDiameter)
        R = D/2.0
        if self.type==0:
            if self.blunt=='spherical':
                return conical_spherically_blunted2(L,R,self.Rnose)
            elif self.blunt=='flat':
                return conical_flat(L,R,self.Rnose)
            else:
                return conical(L,R)
        elif self.type==1:
            if self.blunt=='spherical':
                return tangent_ogive_spherically_blunted2(L,R,self.Rnose)
            elif self.blunt=='flat':
                return tangent_ogive_flat(L,R,self.Rnose)
            else:
                return tangent_ogive(L,R)
        elif self.type==2:
            return spherical(R)
        elif self.type==3:
            return parabolic(L,R,self.parameter)
        elif self.type==4:
            return power_series(L,R,self.parameter)
        elif self.type==5:
            return haack_series(L,R,self.parameter)


class NoseWidget():
    """
    Widget that shows airfoil selection menu
    """
    def __init__(self):
        self.widget = QtGui.QWidget()
        self.ui = Ui_nose_profile()
        self.ui.setupUi(self.widget)
        self.initialize_buttons()
        self.nose = None
        self.tail = None
        self.isTail    = False
    
    def initialize_buttons(self):
        self.ui.radioButton.setChecked(True)
        #self.ui.checkBox.setChecked(False)
        self.ui.radioButton.clicked.connect(self._enable_checkbox)
        self.ui.radioButton_2.clicked.connect(self._enable_checkbox)
        self.ui.radioButton_3.clicked.connect(self._disable_checkbox)
        self.ui.radioButton_4.clicked.connect(self._disable_checkbox)
        self.ui.radioButton_5.clicked.connect(self._disable_checkbox)
        self.ui.radioButton_6.clicked.connect(self._disable_checkbox)
        self.ui.pushButton.clicked.connect(self.confirm)
        self.ui.pushButton_2.hide()
    
    def _enable_checkbox(self):
        self.ui.radioButton_7.setEnabled(True)
        self.ui.radioButton_8.setEnabled(True)
        self.ui.radioButton_9.setEnabled(True)
        self.ui.lineEdit_4.setEnabled(True)
    
    def _disable_checkbox(self):
        self.ui.radioButton_7.setEnabled(False)
        self.ui.radioButton_8.setEnabled(False)
        self.ui.radioButton_9.setEnabled(False)
        self.ui.lineEdit_4.setEnabled(False)
        self.ui.radioButton_7.setChecked(True)
    
    def confirm(self):
        output = NoseShape()
        if self.ui.radioButton.isChecked():
            output.type=0
        elif self.ui.radioButton_2.isChecked():
            output.type=1
        elif self.ui.radioButton_6.isChecked():
            output.type=2
        elif self.ui.radioButton_3.isChecked():
            output.type=3
            output.parameter = float(self.ui.lineEdit.text())
        elif self.ui.radioButton_4.isChecked():
            output.type=4
            output.parameter = float(self.ui.lineEdit_2.text())
        elif self.ui.radioButton_5.isChecked():
            output.type=5
            output.parameter = float(self.ui.lineEdit_3.text())

        if self.ui.radioButton_7.isChecked():
            output.blunt = None
        elif self.ui.radioButton_8.isChecked():
            output.blunt = 'spherical'
            output.Rnose = float(self.ui.lineEdit_4.text()) *1.0e3
        elif self.ui.radioButton_9.isChecked():
            output.blunt = 'flat'
            output.Rnose = float(self.ui.lineEdit_4.text()) *1.0e3
        if self.isTail:
            self.tail = output
        else:
            self.nose = output
        self.widget.hide()
